package com.itheima.service.imp;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberDao memberDao;



    @Override
    public Map findMemberCount() {

        Map map = new HashMap();
        /*
         *  获取y轴月份
         *   动态数据获取，月份是固定的所以可以给固定值
         * 动态展示上一年的每月的月份信息
         * */
        List<String> months = new ArrayList();
        Calendar calendar = Calendar.getInstance();
        for(int i=0;i<12;i++){
            calendar.add(Calendar.MONTH,-1);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            String format = sdf.format(calendar.getTime());
            months.add(format);
        }
        map.put("months",months);
            /*
            * 获取每月会员数量
            * */
        List<Integer> memberCount = new ArrayList();
        for (String month : months) {
          Integer members =  memberDao.findMeberCountById(month);
          memberCount.add(members);
        }

        map.put("memberCount",memberCount);

        return map;
    }

    @Override
    public Map<String, Object> getSetmeaReport() {
        Map<String,Object> result = new HashMap();

        List<Map<String,Object>> list = memberDao.getSetmeaReport();
        result.put("setmealCount",list);
//            获取套餐名
        List<String> stmealName = new ArrayList<>();
        for (Map<String, Object> objectMap : list) {
            String vaule = (String) objectMap.get("name");
            stmealName.add(vaule);
        }
        result.put("setmealNames",stmealName);

        return result;
    }
}
